/*
 * time_pwm.c
 *
 *  Created on: 2022年7月31日
 *      Author: 四维度
 */
#include "time_pwm.h"

//TIMER_PWM_GROUP0_Init(TIMER2_BASE_ADDR);
void TIMER_PWM_GROUP0_Init(u32 timer_sel)
{
    //外设初始化200Mhz/512/200/2=20000  /2是中心对齐模式
    INTDEV_SET_CLK_RST(timer_sel,(INTDEV_RUN|INTDEV_IS_GROUP0|INTDEV_CLK_IS_CORECLK_DIV4));
    TIMER_SET_OVERRIDE_GPIO(timer_sel,(
            TIMER_P0_OVERRIDE_GPIO|
            TIMER_P1_OVERRIDE_GPIO|
            TIMER_P2_OVERRIDE_GPIO
            ));
    //输出使能
    TIMER_SET_OUTPUT_EN(timer_sel,(
            TIMER_P0_OUTPUT_ENABLE|
            TIMER_P1_OUTPUT_ENABLE|
            TIMER_P2_OUTPUT_ENABLE|

            TIMER_P3_OUTPUT_DISABLE|
            TIMER_P4_OUTPUT_DISABLE|
            TIMER_P5_OUTPUT_DISABLE|
            TIMER_P6_OUTPUT_DISABLE|
            TIMER_P7_OUTPUT_DISABLE
            ));

    TIMER_SET_OUT_PORT(timer_sel,(
            TIMER_P0_IS_COMPARER0|
            TIMER_P1_IS_COMPARER1|
            TIMER_P2_IS_COMPARER2
            ));

    TIMER_SET_COMPARER_MODE(timer_sel,(
            TIMER_COMPARER0_NOT_FORCE|
            TIMER_COMPARER1_NOT_FORCE|
            TIMER_COMPARER2_NOT_FORCE
            ));

    //设置2个规则
    TIMER_SET_MAIN_CNT_BEGIN_VALUE0(timer_sel,TIMER_MAIN_CNT_COUNT_UP,0);
    TIMER_SET_MAIN_CNT_END_VALUE0(timer_sel,TIMER_PWM_GROUP0_MAX_COUNT);
    TIMER_SET_MAIN_CNT_BEGIN_VALUE1(timer_sel,TIMER_MAIN_CNT_COUNT_DOWN,TIMER_PWM_GROUP0_MAX_COUNT);
    TIMER_SET_MAIN_CNT_END_VALUE1(timer_sel,0);

    TIMER_SET_CMD(timer_sel,TIMER_CMD_RESTART);
    TIMER_SET_CMD(timer_sel,TIMER_CMD_RUN);
}
void TIMER_PWM_SET_GROUP0_CCR123(u32 timer_sel,u32 value1,u32 value2,u32 value3)
{
    //比较器0：
    //规则0 写0，写1条件
    //从最小值增计数，先写0
    TIMER_SET_COMPARER0_VALUE0_0(timer_sel,TIMER_COMPARER_OUTPUT0,0);
    TIMER_SET_COMPARER0_VALUE0_1(timer_sel,TIMER_COMPARER_OUTPUT1,value1);
    //规则1 写0，写1条件
    //从最大值减计数，先写1
    TIMER_SET_COMPARER0_VALUE1_1(timer_sel,TIMER_COMPARER_OUTPUT1,TIMER_PWM_GROUP0_MAX_COUNT);
    TIMER_SET_COMPARER0_VALUE1_0(timer_sel,TIMER_COMPARER_OUTPUT0,value1);


    //比较器1：
    //规则0 写0，写1条件
    //从最小值增计数，先写0
    TIMER_SET_COMPARER1_VALUE0_0(timer_sel,TIMER_COMPARER_OUTPUT0,0);
    TIMER_SET_COMPARER1_VALUE0_1(timer_sel,TIMER_COMPARER_OUTPUT1,value2);
    //规则1 写0，写1条件
    //从最大值减计数，先写1
    TIMER_SET_COMPARER1_VALUE1_1(timer_sel,TIMER_COMPARER_OUTPUT1,TIMER_PWM_GROUP0_MAX_COUNT);
    TIMER_SET_COMPARER1_VALUE1_0(timer_sel,TIMER_COMPARER_OUTPUT0,value2);

    //比较器2：
    //规则0 写0，写1条件
    //从最小值增计数，先写0
    TIMER_SET_COMPARER2_VALUE0_0(timer_sel,TIMER_COMPARER_OUTPUT0,0);
    TIMER_SET_COMPARER2_VALUE0_1(timer_sel,TIMER_COMPARER_OUTPUT1,value3);
    //规则1 写0，写1条件
    //从最大值减计数，先写1
    TIMER_SET_COMPARER2_VALUE1_1(timer_sel,TIMER_COMPARER_OUTPUT1,TIMER_PWM_GROUP0_MAX_COUNT);
    TIMER_SET_COMPARER2_VALUE1_0(timer_sel,TIMER_COMPARER_OUTPUT0,value3);


//    TIMER_SET_CMD(timer_sel,TIMER_CMD_RESTART);
    TIMER_SET_CMD(timer_sel,TIMER_CMD_RUN);
}
